package com.weetop.cfw.mine.fragment

import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.LeaseSaleInformationPresenterImp
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.base.view.LeaseSaleInformationView
import com.weetop.cfw.bean.MyLeaseSaleInformationListBean
import com.weetop.cfw.event.RequestLeaseSaleEvent
import com.weetop.cfw.mine.adapter.AllLeaseSaleInformationAdapter
import com.weetop.cfw.mine.adapter.LeaseSaleInformationAdapter
import kotlinx.android.synthetic.main.fragment_lease_sale_information.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LeaseSaleInformationFragment: CommonBaseFragment(),LeaseSaleInformationView,OnRefreshLoadMoreListener {

    private lateinit var leaseSaleInformationAdapter: LeaseSaleInformationAdapter
    private lateinit var allLeaseSaleInformationAdapter:AllLeaseSaleInformationAdapter

    private lateinit var leaseSaleInformationPresenterImp: LeaseSaleInformationPresenterImp

    private var myLeaseSaleInformationDataList:ArrayList<MyLeaseSaleInformationListBean.DataBean> = ArrayList()

    var leaseSaleInformationType:Int = -1 //出租出售数据类型 -1:全部 1:待审核 0:已发布

    private var currentPageNumber = 1//当前数据的页数
    private var pageSize = 10//每页多少条数据
    private var totalCount = 0//数据总条数

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否正在加载更多


    override fun getLayoutId() = R.layout.fragment_lease_sale_information

    override fun initView() {
          leaseSaleInformationPresenterImp = LeaseSaleInformationPresenterImp()
          leaseSaleInformationPresenterImp.attachView(this)
          smartRefreshLayout.setOnRefreshListener(this)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        leaseSaleInformationPresenterImp.detachView()
    }

    override fun initData() {
        if(leaseSaleInformationType == 0 || leaseSaleInformationType == 1){
            leaseSaleInformationAdapter = LeaseSaleInformationAdapter(R.layout.layout_item_unaudited_lease_sale_information,myLeaseSaleInformationDataList)
            leaseSaleInformationAdapter.emptyView = emptyView
            leaseSaleInformationRV.adapter = leaseSaleInformationAdapter
        }else{
            allLeaseSaleInformationAdapter = AllLeaseSaleInformationAdapter(myLeaseSaleInformationDataList)
            allLeaseSaleInformationAdapter.emptyView = emptyView
            leaseSaleInformationRV.adapter = allLeaseSaleInformationAdapter
        }
    }

    override fun onResume() {
        super.onResume()
        if(leaseSaleInformationType == -1){
            userVisibleHint = true
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        if(isFirstLoadData){
            leaseSaleInformationPresenterImp.getMyLeaseSaleInformationDataList(this,leaseSaleInformationType,currentPageNumber,pageSize)
        }
    }

    /**
     * 出租出售信息获取成功
     */
    override fun getMyLeaseSaleInformationSuccess(leaseSaleInformationListBean: MyLeaseSaleInformationListBean?) {
        if (leaseSaleInformationListBean != null){

            totalCount = leaseSaleInformationListBean.records

            isFirstLoadData = false
            if(isRefresh){//处于刷新状态
                myLeaseSaleInformationDataList.clear()
                myLeaseSaleInformationDataList.addAll(leaseSaleInformationListBean.data)
                leaseSaleInformationAdapter.notifyDataSetChanged()
                isRefresh = false
                smartRefreshLayout.finishRefresh(true)
                if(leaseSaleInformationListBean.data.size > 0){
                    currentPageNumber = 1//将当前数据页数重置为1
                }
                return
            }
            if(isLoadMore){
                myLeaseSaleInformationDataList.addAll(leaseSaleInformationListBean.data)
                leaseSaleInformationAdapter.notifyDataSetChanged()
                isLoadMore = false
                smartRefreshLayout.finishLoadMore(true)
                if(leaseSaleInformationListBean.data.size > 0){
                    currentPageNumber += 1//将当前数据页数增加1
                }
                return
            }
          myLeaseSaleInformationDataList.addAll(leaseSaleInformationListBean.data)
            leaseSaleInformationAdapter.notifyDataSetChanged()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveRequestLeaseSaleEvent(requestLeaseSaleEvent: RequestLeaseSaleEvent){
        if(requestLeaseSaleEvent.currentItem != 0){
            userVisibleHint = true
        }
    }

    /**
     * 出租出售信息获取成功
     */
    override fun getMyLeaseSaleInformationFailed() {
        smartRefreshLayout.finishRefresh(false)
        smartRefreshLayout.finishLoadMore(false)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= myLeaseSaleInformationDataList.size){
            showNativeShortToast("暂无更多数据")
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            leaseSaleInformationPresenterImp.getMyLeaseSaleInformationDataList(this,leaseSaleInformationType,currentPageNumber + 1,pageSize)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        leaseSaleInformationPresenterImp.getMyLeaseSaleInformationDataList(this,leaseSaleInformationType,1,pageSize)
    }
}